package com.elin4it.blog.service;

import com.elin4it.blog.controller.exception.BlogException;
import com.elin4it.blog.mapper.ArticleCustomMapper;
import com.elin4it.blog.mapper.ArticleMapper;
import com.elin4it.blog.mapper.ArticleTypeMapper;
import com.elin4it.blog.pojo.Article;
import com.elin4it.blog.pojo.ArticleCustom;
import com.elin4it.blog.pojo.ArticleExample;
import com.elin4it.blog.pojo.ArticleType;
import com.elin4it.blog.util.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * User:Elin
 * Date:2015/7/19
 * Description:
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleCustomMapper articleCustomMapper;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleTypeMapper articleTypeMapper;
    @Autowired
    private Properties config;

    public int addArticle(Article article) throws Exception {
        articleCustomMapper.insert(article);
        int id = article.getId();
        return id;
    }

    public Integer articleCount(int type) throws Exception {
        ArticleExample articleExample = new ArticleExample();
        ArticleExample.Criteria criteria = articleExample.createCriteria();
        criteria.andTypeEqualTo(type);

        Integer articleCount = null;

        //如果type为0表示无条件查找
        if (type == 0) {
            articleCount = articleMapper.countByExample(null);
        } else {
            articleCount = articleMapper.countByExample(articleExample);
        }

        return articleCount;
    }

    public ArticleCustom findArticle(int id) throws Exception {
        Article article = articleMapper.selectByPrimaryKey(id);
        if (article == null)
            throw new BlogException(config.getProperty("blogNotFound"));
        ArticleCustom articleCustom = conversion(article);

        return articleCustom;
    }

    public List<ArticleCustom> findArticleTopTenByTime() throws Exception {

        //得到默认的最新文章的数量
        int hotPageCount = Integer.parseInt(config.getProperty("hotPageCount"));
        //从数据库得到Article类型的文章列表
        List<Article> list = articleCustomMapper.findArticleTopByTime(hotPageCount);
        //将Article转换为ArticleCustom
        List<ArticleCustom> customList = conversionList(list);
        return customList;
    }

//    public List<ArticleCustom> findArticlePaging(int pageNum, int pageCount) throws Exception {
//        Page<Article> page = new Page<Article>();
//        page.setPageNo(pageNum);
//        page.setPageSize(pageCount);
//        List<Article> list = articleCustomMapper.findArticle(page);
//        List<ArticleCustom> customList = conversionList(list);
//
//        return customList;
//    }

    public List<ArticleCustom> findArticle() throws Exception {
        return null;
    }

    public Integer pageNum(int type) throws Exception {
        int pageCount = Integer.parseInt(config.getProperty("pageCount"));
        int articleCount = articleCount(type);
        int result = articleCount / pageCount;
        Integer allPageConut = (result == (double) articleCount / pageCount ? result : result + 1);
        return allPageConut;
    }

    public List<ArticleCustom> findArticlePagingWithArticleType(int pageNum, int pageCount, int type) throws Exception {

        //第一个参数为页码，第二个参数为要获得的记录数目
        PageHelper.startPage(pageNum, pageCount);
        //紧跟着的第一个select方法会被分页


        List<Article> list = null;
        //设置查询条件
        ArticleExample articleExample = new ArticleExample();
        ArticleExample.Criteria criteria = articleExample.createCriteria();
        //通过id逆序排列
        articleExample.setOrderByClause("id desc");
        if (type == 0) {
            //type=0表示无条件查询
            list = articleMapper.selectByExample(articleExample);
        } else {
            //设置文章类型查询
            criteria.andTypeEqualTo(type);
            list = articleMapper.selectByExample(articleExample);
        }


        List<ArticleCustom> customList = conversionList(list);

        return customList;
    }

    public boolean updateArticle(Article article) throws Exception {
        //执行修改操作
        int result = articleMapper.updateByPrimaryKeyWithBLOBs(article);
        return result > 0;
    }

    public boolean deleteArticle(Integer id) throws Exception {
        return articleMapper.deleteByPrimaryKey(id)>0;
    }

    /**
     * 将Article转换为ArticleCustom
     *
     * @param article
     * @return
     * @throws Exception
     */
    private ArticleCustom conversion(Article article) throws Exception {
        ArticleCustom articleCustom = new ArticleCustom();
        //拷贝文章的信息
        BeanUtils.copyProperties(article, articleCustom);

        //时间格式化
        if (articleCustom.getSubmitTime() != null) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            articleCustom.setFormatTime(simpleDateFormat.format(articleCustom.getSubmitTime()));
        }


        //根据id查询
        ArticleType articleType = articleTypeMapper.selectByPrimaryKey(articleCustom.getType());
        if (articleType == null || articleType.getId() == null) {
            throw new BlogException(config.getProperty("typeNotFound"));
        }
        //得到类型名
        String typeName = articleType.getName();

        articleCustom.setTypeName(typeName);

        return articleCustom;
    }

    /**
     * 将Article List转换为ArticleCustom List
     *
     * @param list
     * @return
     * @throws Exception
     */
    private List<ArticleCustom> conversionList(List<Article> list) throws Exception {
        List<ArticleCustom> customList = new ArrayList<ArticleCustom>();
        //逐个拷贝数据
        for (Article article : list) {
            ArticleCustom articleCustom = conversion(article);

            customList.add(articleCustom);
        }
        return customList;
    }
}
